<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link type="text/css" rel="stylesheet" href="jsmind.css" />

        <script type="text/javascript" src="jsmind.js"></script>
        <script type="text/javascript" src="jsmind.draggable.js"></script>
        <script type="text/javascript" src="jsmind.screenshot.js"></script>
    </head>
    <body>
        <div id="jsmind_container"></div>

        <script type="text/javascript">
         var _jm = null;

         var options = {
             container:'jsmind_container',
             editable:true
         }

         function select_root_node() {
             _jm.select_node("root");
         }

         function init_root_node() {
             _jm = jsMind.show(options);
             select_root_node();
         }

         function init_background(color) {
             document.getElementById("jsmind_container").style.backgroundColor = color;
         }

         function get_root_node_topic() {
             return _jm.get_node("root").topic;
         }

         function get_node_topic() {
             var selected_node = _jm.get_selected_node(); // as parent of new node
             if (selected_node) {
                 return selected_node.topic;
             } else {
                 return ""
             }
         }

         function add_sub_node(){
             var selected_node = _jm.get_selected_node(); // as parent of new node
             if(!selected_node) {
                 _jm.move_node(selected_id,'_first_');
             }

             var nodeid = jsMind.util.uuid.newid();
             var node = _jm.add_node(selected_node, nodeid, 'Topic');
             _jm.select_node(node);
         }

         function add_brother_node(){
             var selected_node = _jm.get_selected_node(); // as parent of new node
             if(!selected_node) {
                 _jm.move_node(selected_id,'_first_');
             }
             if(_jm.view.is_editing()) {
                 _jm.end_edit();
             } else {
                 var nodeid = jsMind.util.uuid.newid();
                 var node = _jm.insert_node_after(selected_node, nodeid, 'Topic');
                 _jm.select_node(node);
             }
         }

         function get_selected_nodeid(){
             var selected_node = _jm.get_selected_node();
             if(!!selected_node){
                 return selected_node.id;
             }else{
                 return null;
             }
         }

         function remove_node(){
             var selected_id = get_selected_nodeid();
             if(selected_id) {
                 _jm.select_node(_jm.get_selected_node().parent);
                 _jm.remove_node(selected_id);
             }
         }

         function update_node_topic(topic) {
             var selected_id = get_selected_nodeid();
             if (selected_id) {
                 _jm.update_node(selected_id, topic);
             }
         }

         function update_node_topic_inline() {
             var selected_id = get_selected_nodeid();
             if (selected_id) {
                 _jm.begin_edit(selected_id);
             }
         }

         function zoom_in() {
             _jm.view.zoom_in();
         };

         function zoom_out() {
             _jm.view.zoom_out();
         };

         function zoom_reset() {
             _jm.view.set_zoom(1);
         }

         function select_up_node(){
             var selected_node = _jm.get_selected_node();
             if(!!selected_node){
                 var up_node = _jm.find_node_before(selected_node);
                 if(!up_node){
                     var np = _jm.find_node_before(selected_node.parent);
                     if(!!np && np.children.length > 0){
                         up_node = np.children[np.children.length-1];
                     }
                 }
                 if(!!up_node){
                     _jm.select_node(up_node);
                 }
             }
         }

         function select_down_node(){
             var selected_node = _jm.get_selected_node();
             if(!!selected_node){
                 var down_node = _jm.find_node_after(selected_node);
                 if(!down_node){
                     var np = _jm.find_node_after(selected_node.parent);
                     if(!!np && np.children.length > 0){
                         down_node = np.children[0];
                     }
                 }
                 if(!!down_node){
                     _jm.select_node(down_node);
                 }
             }
         }

         function select_left_node(){
             select_relative_node(-1);
         }

         function select_right_node(){
             select_relative_node(1);
         }

         function select_relative_node(d){
             var selected_node = _jm.get_selected_node();
             var node = null;
             if(!!selected_node){
                 if(selected_node.isroot){
                     var c = selected_node.children;
                     var children = [];
                     for(var i=0;i<c.length;i++){
                         if(c[i].direction === d){
                             children.push(i)
                         }
                     }
                     node = c[children[Math.floor((children.length-1)/2)]];
                 }
                 else if(selected_node.direction === d){
                     var children = selected_node.children;
                     var childrencount = children.length;
                     if(childrencount > 0){
                         node = children[Math.floor((childrencount-1)/2)]
                     }
                 }else{
                     node = selected_node.parent;
                 }
                 if(!!node){
                     _jm.select_node(node);
                 }
             }
         }

         function node_is_focus() {
             return _jm.view.is_editing();
         }

         function toggle_node() {
             var selected_node = _jm.get_selected_node(); // as parent of new node
             if (selected_node) {
                 _jm.toggle_node(selected_node);
             }
         }

         function save_screenshot() {
             _jm.screenshot.shootDownload();
         }

         function open_file(file_base64_content) {
             _jm = jsMind.show(options);

             var file_data = decodeURIComponent(escape(window.atob(file_base64_content)));
             var mind = jsMind.util.json.string2json(file_data);

             if(!!mind){
                 _jm.show(mind);

                 select_root_node();
             }
         }

         function save_file() {
             return jsMind.util.json.json2string(_jm.get_data());
         }

         function change_node_background(image) {
             var selected_id = get_selected_nodeid();
             if(selected_id) {
                 _jm.set_node_background_image(selected_id, "file://" + image);
             }
         }

         function relayout() {
             _jm.view.relayout();
         }

         function paste_node_tree(src_node_id) {
             /* var selected_node = _jm.get_selected_node();
              * if(!!selected_node){
              *     var src_node = _jm.get_node(src_node_id);
              *     _jm.move_node(src_node, selected_node, selected_node.direction);
              * } */

             var selected_node = _jm.get_selected_node();
             if(!!selected_node){
                 var src_node = _jm.get_node(src_node_id);
                 _jm.move_node(src_node, src_node_id, selected_node.id, selected_node.direction);
             }
         }

         function remove_middle_node() {
             var selected_node = _jm.get_selected_node();
             if(!!selected_node){
                 var children = selected_node.children;
                 for (var i = 0; i < children.length; i++) {
                     var child = children[i];
                     _jm.move_node(child, child.id, selected_node.parent.id, selected_node.parent.direction);
                 }

                 _jm.select_node(selected_node.parent);
                 _jm.remove_node(selected_node.id);
             }
         }

         function add_middle_node() {
             var selected_node = _jm.get_selected_node(); // as parent of new node
             if(!!selected_node) {
                 var topic = selected_node.topic;

                 add_brother_node();

                 var brother_node = _jm.get_selected_node();
                 _jm.update_node(brother_node.id, topic);
                 _jm.update_node(selected_node.id, "Topic");

                 _jm.move_node(selected_node, selected_node.id, brother_node.id, brother_node.direction);

                 _jm.select_node(selected_node);
             }
         }
        </script>
    </body>
</html>
